home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Games / MoofWars / MoofWars Encoder 8⁄15⁄96 / •Sources / ICL8Encode.cp < prev    next >
Encoding:
Text File  |  1996-08-19  |  3.1 KB  |  116 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #    ICL8Encode.cp
  4. #
  5. #    Author: Timothy Carroll
  6. #    Apple Developer Technical Support
  7. #    timc@apple.com
  8. #
  9. #    Modification History: 
  10. #
  11. #    8/15/96        TMC     Initial Release
  12. #
  13. #    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  14. #
  15. #
  16. #    You may incorporate this sample code into your applications without
  17. #    restriction, though the sample code has been provided "AS IS" and the
  18. #    responsibility for its operation is 100% yours.  However, what you are
  19. #    not permitted to do is to redistribute the source as "DSC Sample Code"
  20. #    after having made changes. If you're going to re-distribute the source,
  21. #    we require that you make it clear in the source that the code was
  22. #    descended from Apple Sample Code, but that you've made changes.
  23. #
  24. *************************************************************************************/
  25.  
  26. #include "TGraphic.h"
  27. #include "ICL8Encode.h"
  28.  
  29. OSStatus ICL8Encode (short inputFileResNum, short outputFileResNum)
  30. {
  31.     OSStatus        theErr;
  32.  
  33.     UInt16            numIcons, loop;
  34.  
  35.     // we pass these to GetResInfo so that we can get the actual resource ID.
  36.     SInt16            resID;
  37.     ResType            resType;
  38.     Str255            resName;
  39.  
  40.     // Holds the last value on the resource chain since we change the top resource a lot
  41.     SInt16            saveResNum;
  42.  
  43.     // Temporarily holds the resource so we can get information on it
  44.     Handle             icon = NULL;
  45.     TGraphic         *compiled = NULL;
  46.         
  47.     saveResNum = CurResFile();
  48.  
  49.     UseResFile (inputFileResNum);
  50.  
  51.  
  52.     // First thing is to copy the color table resource to the output.
  53.     theErr = CopyResource (inputFileResNum, outputFileResNum,'clut', kAppColorTableResID);
  54.     FAIL_OSERR (theErr, "\pFailed to copy the color table to the destination file")
  55.  
  56.  
  57.     // get the color table
  58.     gAppColorTable = GetCTable( kAppColorTableResID );
  59.     FAIL_NIL (gAppColorTable, "\pFailed to open the color table")
  60.  
  61.     // determine the number of icl8 resources
  62.     numIcons = Count1Resources( 'icl8' );
  63.  
  64.     
  65.     // get each one,
  66.     for( loop = 1; loop <= numIcons; loop++ )
  67.     {
  68.         // load the icon
  69.         UseResFile (inputFileResNum);
  70.                     
  71.         // we'll get the icon first so that we can get the information out of it.
  72.         icon = Get1IndResource( 'icl8', loop );
  73.         theErr = ResError();
  74.         FAIL_NIL (icon, "\pFailed to load the icon")
  75.         FAIL_OSERR (theErr, "\pFailed to load the icon")
  76.             
  77.         // determine its id and name
  78.         GetResInfo( icon, &resID, &resType, resName );
  79.         theErr = ResError();
  80.         FAIL_OSERR( theErr, "\pFailed to get info on the resource")
  81.         
  82.         ReleaseResource (icon);
  83.         
  84.         // Okay, we know the icl8's resID, build the compiled graphic and write it to the output file.
  85.         compiled = TGraphic::NewGraphic (resID);
  86.         FAIL_NIL (compiled, "\pFailed to compile the TGraphic")
  87.         
  88.         UseResFile (outputFileResNum);
  89.         
  90.         compiled->WriteToGraphicResource();
  91.         compiled->WriteToPICTResource();
  92.         compiled->DisposeReference();
  93.  
  94.     }
  95.     // restore the previous port and device
  96.     
  97.     goto cleanup;
  98.     
  99. error:
  100.     
  101.     if (theErr == noErr)
  102.         theErr = paramErr;
  103. cleanup:
  104.     
  105.     UseResFile (saveResNum);
  106.     
  107.     if (icon)
  108.         ReleaseResource (icon);
  109.     if (compiled)
  110.         compiled->DisposeReference();
  111.     
  112.     if (gAppColorTable != NULL)
  113.         DisposeCTable (gAppColorTable);
  114.     gAppColorTable = NULL;
  115.     return theErr;
  116. }